<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Order extends AdminController{
	public $_validation = null;
	
	function __construct(){
	    date_default_timezone_set('Asia/Shanghai');
		parent::__construct();
		$this->load->model('MSelfBankOrder');
		$this->load->model('MOrder');
		$this->pay_status_css = array(
				'0'=>'label label-sm label-default',
				'1'=>'label label-sm label-success'
		);
		$this->transfer_status_css = array(
				'0'=>'label label-sm label-default',
				'1'=>'label label-sm label-success',
				'2'=>'label label-sm label-warning'
		);
		$this->_validation = Array(
			array('field'=>'no', 'label'=>'订单号', 'rules'=>'required|max_length[16]')
			,array('field'=>'user_id', 'label'=>'用户编号', 'rules'=>'required|integer|max_length[11]')
			,array('field'=>'amount', 'label'=>'订单数量', 'rules'=>'required')
			,array('field'=>'pay_status', 'label'=>'自有通道状态', 'rules'=>'integer')
			,array('field'=>'pay_time', 'label'=>'自有通道确认时间', 'rules'=>'integer')
			,array('field'=>'pay_channel', 'label'=>'自有通道', 'rules'=>'max_length[32]')
			,array('field'=>'pay_transaction', 'label'=>'自有通道流水号', 'rules'=>'max_length[32]')
			,array('field'=>'transfer_platform', 'label'=>'平台', 'rules'=>'max_length[32]')
			,array('field'=>'transfer_status', 'label'=>'平台状态', 'rules'=>'integer|max_length[4]')
			,array('field'=>'transfer_time', 'label'=>'平台确认时间', 'rules'=>'integer')
			,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
		);
	}
	/**
	 * 一般订单列表
	 * @param unknown $game_tag
	 */
	function index($game_tag=null){
		//$this->output->enable_profiler(TRUE);
		//$this->load->model('MUser');
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('no', 'where');
		$this->formfilter->addFilter('user_id','where');
		$this->formfilter->addFilter('user.account','where');
		$this->formfilter->addFilter('pay_status','where');
		$this->formfilter->addFilter('transfer_status','where');
		//最新一条记录
		$lastItem = $this->MOrder->getLast();
		$limit = $this->pagination($this->MOrder->getCount());
		$list = $this->MOrder->getList($limit);
		$this->assign('list', $list);
		$this->assign('lastItem', $lastItem);
		$this->layout();
	}
	
	
	function view($id){
	       
	    if(!$this->p->edit){
	        //权限不足
	        show_error(l('user_has_nopower'));
	    }
		if(!is_numeric($id)){
			show_error(l('id_not_null'));
		}
		$obj = $this->MOrder->getOne(Array('id'=>$id));
		if(!$obj){
			errorAndRedirect('未找到定义！');
		}
		$this->load->model('MOrderLog');
		$orderLogList = $this->MOrderLog->getAll(Array('order_id'=>$id));
		$this->load->model('MUser');
		$userInfo = $this->MUser->getOne(array('id'=>$obj->user_id));
		$this->load->model('MPaymentConfig');
		$paymentType = $this->MPaymentConfig->getByCode($obj->pay_channel);
		$this->assign('order', $obj);
		$this->assign('paymentType', $paymentType);
		$this->assign('orderLogList', array_reverse($orderLogList));
		$this->assign('userInfo', $userInfo);
		$this->layout('view');
	}
	
	function recharge($id,$update_time){
		if(!is_numeric($id) || !is_numeric($update_time)){
			show_error(l('id_not_null'));
		}
		$redirectUrl = site_url($this->_thisModule.$this->_thisController.'/view/'.$id);
		$order = $this->MOrder->getOne(Array('id'=>$id));
		if(!$order){
			errorAndRedirect('未找到定义！',$redirectUrl);
		}
		if($update_time != $order->update_time){
			errorAndRedirect('操作冲突，请重试', $redirectUrl);
		}
		if ($order->pay_status != IOSS_Order::PAY_STATE_PAID || $order->transfer_status != IOSS_Order::PAY_STATE_NOT_RECHARGED){
			//订单状态为必须是已付款、未充值才可以进行充值操作
			errorAndRedirect('订单状态不正确', $redirectUrl);
		}
		
		//调用充值接口给游戏充值----------------------------------------------
		$user = IOSS_User::getById($order->user_id);
		$billno = date('ymdHis') . rand(1000,9999);
		// 预转账
		if(!IOSS_AGApi::agin_prepare_transfer_credit($user->account, $user->game_key, $billno, 'IN', $order->amount, $prepare_error_message)){
		    errorAndRedirect('平台预操作失败', $redirectUrl); // 预转账失败
		}
		// 转账确认
		if (!IOSS_AGApi::agin_confirm_transfer_credit($user->account, $user->game_key, $billno, 'IN', $order->amount, $confirm_error_message)){
		    if ($confirm_error_message['Type'] === 'network_error') {
		        if (!IOSS_AGApi::agin_query_transfer_status($billno, $query_error_message)) {
		            errorAndRedirect('平台操作状态确认未成功', $redirectUrl);
		        }
		    }else{
		        errorAndRedirect('平台操作失败', $redirectUrl);;
		    }
		}
		//--------------------------------------------------------------
		
		//修改订单状态
		$time = time();
		$order->transfer_status = IOSS_Order::PAY_STATE_RECHARGED;
		$order->transfer_time = $time;
		if(!$this->MOrder->update($order)){
			errorAndRedirect('修改订单状态失败', $redirectUrl);
		}
		//操作日志添加记录
		$this->load->model('MOrderLog');
		$newLog = new stdClass();
		$newLog->order_id = $order->id;
		$newLog->pay_status = $order->pay_status;
		$newLog->transfer_status = $order->transfer_status;
		$newLog->admin_id = $this->_user->id;
		$newLog->admin_account = $this->_user->account;
		$newLog->admin_name = $this->_user->name;
		$newLog->note = '【客服手动转入平台操作】';
		$newLog->create_time = $time;
		$this->MOrderLog->add($newLog);
		successAndRedirect('平台操作确认成功',$redirectUrl);		
	}
	
	function delete($id, $update_time){
		if(!is_numeric($id) || !is_numeric($update_time)){
			show_error(l('id_not_null'));
		}
		$this->load->model('MGameTags');
		
		$this->db->trans_start();
		if(!$this->MGame->delete($id, $update_time)){
			$this->db->_trans_status = FALSE;
		}
		if(!$this->MGameTags->deleteGameId($id)){
			$this->db->_trans_status = FALSE;
		}
		$this->db->trans_complete();
		if($this->db->trans_status()!==FALSE){
			successAndRedirect(l('delete_success'));
		}else{
			errorAndRedirect(l('data_fail'));
		}
	}
	/**
	 * ajax请求最新的记录id
	 */
	public function pollingOrderLastId(){
	    $lastItem = $this->MOrder->getLast();
	    $retObj = new stdClass();
	    $retObj->lastId = $lastItem->id;
	    echo json_encode($retObj);
	}
	
}